package com.jingdong.controller;

import com.jingdong.model.*;
import com.jingdong.service.address.CityService;
import com.jingdong.service.address.CountyService;
import com.jingdong.service.address.OrderAddressService;
import com.jingdong.service.address.ProvinceService;
import com.jingdong.service.order.OrderItemService;
import com.jingdong.service.order.OrderService;
import com.jingdong.service.product.ProductService;
import com.jingdong.service.shoppingCart.ShoppingCartService;
import com.jingdong.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CityService cityService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private OrderAddressService orderAddressService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ShoppingCartService cartService;
    @RequestMapping("toSubmitOrder")
    public ModelAndView toSubmitOrder(String id, String count, String totalPrice,String cartId,Integer uid){
        ModelAndView mav = new ModelAndView("order/submitOrder");
        String[] strId = id.split(",");
        String[] strCount = count.split(",");
        String []strCartId=cartId.split(",");
        List<ProductVo> voList=new ArrayList<ProductVo>();
        List<Order_address> addressList=orderAddressService.addressList(uid);
        for (int i=0;i<strId.length;i++){
            ProductVo vo=new ProductVo();
            Product product=productService.getById(Integer.parseInt(strId[i]));
            vo.setProduct_name(product.getProduct_name());
            vo.setSize(product.getSize());
            vo.setResolution(product.getResolution());
            vo.setPrice(product.getPrice());
            vo.setPlace(product.getPlace());
            vo.setId(product.getId());
            vo.setType_id(product.getType_id());
            vo.setCount(Integer.parseInt(strCount[i]));
            vo.setCartId(Integer.parseInt(strCartId[i]));
            voList.add(vo);
        }
        mav.addObject("voList", voList);
        mav.addObject("totalPrice", totalPrice);
        mav.addObject("addressList",addressList);
        return mav;
    }
    @RequestMapping("toSaveAddress")
    public ModelAndView toSaveAddress(){
        ModelAndView mav=new ModelAndView("order/saveAddress");
        List<Province> provinceList=provinceService.getProvinceList();
        mav.addObject("provinceList",provinceList);
        return mav;
    }
    @RequestMapping("city")
    public @ResponseBody
    Object cityList(@RequestParam(value = "pid", required = false) int pid) {
        List<City> cityList = cityService.getCityList(pid);
        return cityList;
    }

    @RequestMapping("county")
    public @ResponseBody
    Object countyList(@RequestParam(value = "cid", required = false) int cid) {
        List<County> countyList = countyService.getCountyList(cid);
        return countyList;
    }
    @RequestMapping("saveOrderAddress")
    public ModelAndView saveOrderAddress(Integer id, String receiver, String phone, String address, String district,Long uid){
        String[] location = district.split(",");
        Province province = provinceService.getProvinceById(Integer.parseInt(location[0]));
        City city = cityService.getCityById(Integer.parseInt(location[1]));
        County county = countyService.getCountyById(Integer.parseInt(location[2]));
        Order_address orderAddress=new Order_address();
        orderAddress.setPhone(phone);
        orderAddress.setReceiver(receiver);
        orderAddress.setUid(uid);
        orderAddress.setAddress(province.getP_name() + city.getC_name() + county.getX_name() + address);
        int saveResult=orderAddressService.save(orderAddress);
        return null;
    }
    @RequestMapping("submitOrder")
    public ModelAndView submitOrder(String id, Integer addressId, String totalPrice,
                                    String proCount, String proPrice, Integer payType, String proName, Integer uid, String cartId,String proId) {
        ModelAndView mav = new ModelAndView("/order/orderPayMoney");
        Map<String ,Long>map=new HashMap<String,Long>();
        Order_info order = new Order_info();
        order.setUid(uid.longValue());
        order.setTotal_price(Double.parseDouble(totalPrice));
        order.setAddress_id(addressId.longValue());
        order.setPay_type(payType.longValue());
        order.setUid(uid.longValue());
        order.setPay_status(0L);
        Date date = new Date();
        SimpleDateFormat simpleDateForma = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        order.setCreate_time(simpleDateForma.format(date.getTime()));
        int result = orderService.save(order);
        String[] productCount = proCount.split(",");
        String[] productPrice = proPrice.split(",");
        String[] strCartId = cartId.split(",");
        String[] productId = proId.split(",");
        for (int i = 0; i < productCount.length; i++) {
            Order_items orderItem = new Order_items();
            orderItem.setOrder_id(order.getOrder_id());
            orderItem.setPrice(Double.parseDouble(productPrice[i]));
            orderItem.setProduct_count(Long.parseLong(productCount[i]));
            orderItem.setUid(uid.longValue());
            orderItem.setProduct_id(Long.parseLong(productId[i]));
            int orderItemResult = orderItemService.addOrderItem(orderItem);
            Long productStatus = 0L;
            map.put("productStatus", productStatus);
            map.put("uid", uid.longValue());
            map.put("id",Long.parseLong(strCartId[i]));
            Shopping_cart shopping_cart=cartService.getById(map);
            shopping_cart.setProduct_status(1L);
            cartService.update(shopping_cart);
        }
        mav.addObject("orderId", order.getOrder_id());
        mav.addObject("totalPrice", totalPrice);
        return mav;

    }
    @RequestMapping("payMoney")
    public @ResponseBody
    Object payMoney(Integer orderId, Integer payStatus) {
        Order_info order = orderService.getById(orderId);
        order.setPay_status(payStatus.longValue());
        int payResult = orderService.update(order);
        return order.getPay_status();
    }
    @RequestMapping("orderList")
    public ModelAndView orderList(Integer uid,Integer payStatus){
        Map<String ,Integer>map=new HashMap<String,Integer>();
        if (uid!=null&&!uid.equals("")) {
            map.put("uid", uid);
        }
        if (payStatus!=null&&!payStatus.equals("")){
            map.put("payStatus",payStatus);
        }
        List<Order_info> orderInfoList=orderService.list(map);
        ModelAndView mav=new ModelAndView("order/orderMangerList");
        mav.addObject("orderList",orderInfoList);
        return mav;
    }
    @RequestMapping("updateStatus")
    public ModelAndView updateStatus(Integer uid,Integer orderId){
        Order_info order_info=orderService.getById(orderId);
        order_info.setPay_status(4L);
        int result=orderService.update(order_info);
        return orderList(uid,null);
    }
}
